package com.example.datatrans.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.datatrans.config.ConfigValue;
import com.example.datatrans.dto.FileSourceDTO;
import com.example.datatrans.entity.FileSource;
import com.example.datatrans.mapper.FileSourceMapper;
import com.example.datatrans.service.IFileSourceService;
import com.example.datatrans.util.FileUtils;
import com.example.datatrans.vo.FileSourceVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: cs
 * @date: 2024-10-29 10:59:31
 * @since: 1.0
 */
@Service
public class FileSourceServiceImpl extends ServiceImpl<FileSourceMapper, FileSource> implements IFileSourceService {

    @Resource
    private ConfigValue configValue;

    @Override
    public List<FileSourceVO> storeAllFile() {

        List<FileSourceDTO> fileSourceDTOS = baseMapper.selectAll();
        String fileSourceStorePath = configValue.getFileSourceStorePath();
        File path = new File(fileSourceStorePath);
        if (!path.exists()) {
            boolean ok = path.mkdir();
            if (ok) {
                System.out.println("创建目录成功" + fileSourceStorePath);
            }
        }

        List<FileSourceVO> fileSourceVOList = new ArrayList<>();
        try {
            for (FileSourceDTO dto : fileSourceDTOS) {

                String fileName = dto.getFileName();
                byte[] source = dto.getSource();
                String fileAbsPath = fileSourceStorePath + "\\" + fileName;
                File file = new File(fileAbsPath);
                if (file.createNewFile()) {
                    System.out.println("文件创建成功" + fileAbsPath);
                }
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(source);
                fos.flush();
                fos.close();
                FileSourceVO vo = new FileSourceVO();
                vo.setFileName(fileName);
                vo.setHashcode(dto.getHashcode());
                vo.setFileAbsolutePath(fileAbsPath);
                fileSourceVOList.add(vo);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }

        return fileSourceVOList;
    }

    @Override
    public List<FileSourceVO> findStoredFiles(String storePath) {

        File storeDir = new File(storePath);
        File[] files = storeDir.listFiles();
        if (files == null || files.length == 0) {
            return new ArrayList<>();
        }
        List<FileSourceVO> fileSourceVOList = new ArrayList<>();
        try {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                String name = file.getName();
                String hashCode = FileUtils.generateHashCode(file);
                String fileAbsPath = storePath + "\\" + name;
                FileSourceVO vo = new FileSourceVO();
                vo.setFileName(name);
                vo.setHashcode(hashCode);
                vo.setFileAbsolutePath(fileAbsPath);
                fileSourceVOList.add(vo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return fileSourceVOList;
    }

    @Override
    public boolean backup() {
        String fileSourceStorePath = configValue.getFileSourceStorePath();
        File storeDir = new File(fileSourceStorePath);
        File[] files = storeDir.listFiles();
        if (files == null || files.length == 0) {
            System.err.println("备份文件不存在");
            return false;
        }
        try {
            for (File file : files) {
                String name = file.getName();
                String hashCode = FileUtils.generateHashCode(file);
                byte[] source = FileUtils.readFile2ByteArray(file);
                FileSource fileSource = new FileSource();
                fileSource.setHashcode(hashCode);
                fileSource.setSource(source);
                System.out.println("开始插入表记录，文件名：" + name);
                baseMapper.insert(fileSource);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}
